package cn.tannn.ops.jar.controller;

import cn.tannn.jdevelops.annotations.web.mapping.PathRestController;
import cn.tannn.jdevelops.exception.built.BusinessException;
import cn.tannn.jdevelops.jpa.constant.SQLOperator;
import cn.tannn.jdevelops.jpa.result.JpaPageResult;
import cn.tannn.jdevelops.result.response.ResultPageVO;
import cn.tannn.jdevelops.result.response.ResultVO;
import cn.tannn.ops.jar.controller.pojo.AppJarAdd;
import cn.tannn.ops.jar.controller.pojo.AppJarEdit;
import cn.tannn.ops.jar.controller.pojo.AppJarPage;
import cn.tannn.ops.jar.entity.AppJarEntity;
import cn.tannn.ops.jar.service.AppJarService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

/**
 * 应用jar管理
 *
 * @author <a href="https://t.tannn.cn/">tan</a>
 * @version V1.0
 * @date 2024/9/4 下午3:57
 */
@PathRestController("/jar/app")
@Tag(name = "应用jar管理")
@Slf4j
@RequiredArgsConstructor
public class AppJarController {

    private final AppJarService appJarService;

    @Operation(summary = "查询jar")
    @PostMapping("page")
    public ResultPageVO<AppJarEntity, JpaPageResult<AppJarEntity>> findPage(@RequestBody @Valid AppJarPage page) {
        Page<AppJarEntity> customers = appJarService.findPage(page, page.getPage());
        JpaPageResult<AppJarEntity> pageResult = JpaPageResult.toPage(customers);
        return ResultPageVO.success(pageResult, "查询成功");
    }

    @Operation(summary = "新增jar")
    @PostMapping("append")
    public ResultVO<String> append(@RequestBody @Valid AppJarAdd append) {
        appJarService.findOnly("name", append.getName()).ifPresent(exist -> {
            throw new BusinessException(exist.getName()+"以存在请勿重复添加");
        });
        appJarService.saveOneByVo(append);
        return ResultVO.success();
    }

    @Operation(summary = "编辑jar")
    @PostMapping("edit")
    public ResultVO<String> edit(@RequestBody @Valid AppJarEdit edit) {
        appJarService.update(edit, SQLOperator.EQ);
        return ResultVO.success();
    }


    @Operation(summary = "所有api url 不为空的jar")
    @GetMapping("actuators")
    public ResultVO<List<AppJarEntity>> actuators() {
        // 前端通过 url 查询 actuator/info 显示应用信息
        List<AppJarEntity> finds = appJarService.findApiUriNotNull();
        return ResultVO.success(finds);
    }

    // 删除jar
    // 部署jar
    // 停止jar
    // 重启jar

}
